<template>
  <!-- 经销商列表 -->
  <div style="height:520px">
    <FilterTable
      ref="table"
      height="450"
      :dataSource="dataSource"
      :isToperInfo="false"
      :columns="columns"
      :isSelectColumn="false"
      :fetchapi="fetchParams"
      :params="flterValue"
      :datakey="'records'"
      :onColumnsChange="columns => this.columns = columns"
      columnsRef="projectOperating_projram_commonForm_dealer_01"
    >
      <template slot="controls" v-if="value.type!=='show'">
        <el-button size="small" type="primary" icon="el-icon-plus" @click="openDialog()">添加经销商</el-button>
      </template>
    </FilterTable>
    <!-- 经销商弹窗 -->
    <BaseDialog
      title="经销商信息查询"
      :visible.sync="visibleDialog"
      width="800px"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <dealer-madal
        ref="addDialogRef"
        :deptId="orgId"
        selectType="multiple"
        v-model="initDealerData"
        @close="closeDialog"
      />

      <!-- <dealerDialog    @close="closeDialog" /> -->
    </BaseDialog>
  </div>
</template>

<script>
// import dealerDialog from './dealerDialog'
import { dictionary } from '@/utils/dictMixin';
// import { getLoginUser } from '@/api/cms-api/projectOperating/program/departmentInquire'; //获取登录信息
//经销商窗口
import dealerMadal from './dealerDialogByOrg';
import { getListByDealer } from '@/api/cms-api/projectOperating/program/distribution';
export default {
  mixins: [dictionary],
  components: {
    dealerMadal
  },
  props: {
    value: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      formValue: {},
      editRow: {},
      loginUser: null,
      initDealerData: {}, //经销商弹窗参数
      visibleDialog: false,
      orgId: null,
      initModal: {},
      recordId: '', //查看时，传递过来的数据ID
      flterValue: {}, //参数
      columns: this.creatHeader(),
      fetchParams: null, //ipa 接口
      dataSource: [],
      isOk: false
    };
  },
  created() {
    // this.getUserInfo();
  },
  mounted() {
    //请求接口进行赋值，自己写吧
    if (this.value != null && this.value.type == 'edit') {
      this.init();
    }
  },
  watch: {
    isOk(newval, oldval) {
      if (newval) {
        //如果传入参数不为空，则说明是修改进入，初始化对应数据
        if (this.value != null && this.value.type == 'edit') {
          this.init();
        }
      }
    }
  },
  methods: {
    async getUserInfo() {
      this.loginUser = await getLoginUser({});
      this.orgId = this.loginUser.data.orgId;
    },
    //修改时，初始化页面数据
    async init() {
      let id = this.value.rowData.projectProgramId;
      // 更新接口需要的记录 ID
      this.recordId = id;

      if (id != '' || id != 'undefined') {
        //营销方案ID
        var projectProgramId = this.value.rowData.projectProgramId;
        var projectOrgId = this.value.rowData.projectProgramOrgId; //营销方案事业部关系表ID
        let params = {
          projectProgramId: projectProgramId, //方案ID
          projectOrgId: projectOrgId, //营销方案事业部关系表ID
          currentPage: 1,
          pageNum: 1,
          pageSize: 9999
        };
        //查询所辖经销商列表
        await getListByDealer(params).then(res => {
          //   console.log(res);
          //   debugger
          this.dataSource = res.data.records;
          //   debugger
          //    console.log(this.dataSource)
          this.formValue = this.dataSource;
        });
      }
    },
    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.$index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <el-button size="mini" type="text" onClick={() => this.editHandle(props.$index)}>
                  删除
                </el-button>
              </div>
            );
          }
        },
        { title: '经销商代码', dataIndex: 'dealerCode', sorter: true },
        { title: '经销商简称', dataIndex: 'dealerShortname', sorter: true },
        { title: '经销商全称', dataIndex: 'dealerName', sorter: true }
      ];
    },
    openDialog() {
      // debugger

      this.initDealerData = {
        excludeBigCustomer: false, //是否查询大客户经销商，如果查询 true，不查询 false
        id: 1001
      };
      this.visibleDialog = true;
    },
    closeDialog(data) {
      //返回数据逻辑自己写
      // this.dataSource = data.data;
      //        x.initialValue = this.rowData[x.fieldName];
      if (data.data != undefined) {
        if (0 == data.data.length) {
          this.$notify.warning({ title: '提示信息', message: '请选择数据!' });
          return;
        }
        var isSame = false;
        data.data.forEach(element => {
          for (var i = 0; i < this.dataSource.length; i++) {
            if (this.dataSource[i].dealerCode == element.dealerCode) {

              isSame  =true;
              return;
            }
          }

          this.dataSource.push(Object.assign({}, element));
        });
        if (isSame) {
           this.$notify.warning({ title: '提示信息', message: '经销商重复不可添加!' });
        }

        this.formValue = this.dataSource;

        this.visibleDialog = data.visible; //关闭弹窗
        if (data.type === 'refresh') {
          // 重新加载
          this.flterValue = { ...this.flterValue };
        }
      } else {
        this.visibleDialog = false; //关闭弹窗
      }
    }, //列表删除
    editHandle(index) {
      this.dataSource.splice(index, 1);
      // this.productList.splice(row.index,1);
    }
  }
};
</script>
